package init.utils;

import init.config.Config;

import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionUtils {
	private static Properties config = null;
	private static java.sql.Connection conn = null;
	
	static{
		try {
			config = ConfigUtils.parseConfig();
			
			Class.forName(config.getProperty(Config.DRIVER));
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	
	public static java.sql.Connection getConnection() throws SQLException{
		if(conn==null || conn.isClosed()){
			conn = DriverManager.getConnection(config.getProperty(Config.URL), config.getProperty(Config.USER), config.getProperty(Config.PASSWORD));
		}
		return conn;
	}
	
	public static void release() throws SQLException{
		if(conn!=null && !conn.isClosed()){
			conn.close();
		}
	}
	
	
}
